package com.hc.controller;


import com.hc.bean.CategoryBean;
import com.hc.service.CategoryService;
import com.hc.util.JsonResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Controller
@RequestMapping("/CategoryController")
@CrossOrigin  //允许客户端跨域访问，底层逻辑,往客户端发送http响应头，额外添加Access-Control-Allow-Origin:*响应头，和vary响应头
public class CategoryController {


    @Autowired
    CategoryService categoryService;


 //   @RequestMapping("/queryCategoryByTree",,method = RequestMethod.GET)
    @GetMapping("/queryCategoryByTree")
    @ResponseBody
    public JsonResponse queryCategoryByTree(){
        JsonResponse jsonResponse = new JsonResponse();
        try {
            List<CategoryBean> categoryBeanList = categoryService.queryDataByTree(0);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");
            jsonResponse.setData(categoryBeanList);
        }catch (Exception e){
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("查询分类数据出现异常，原因:"+e.getMessage());

        }

        return  jsonResponse;
    }


    ///CategoryController/addCateGory?category_id=xxx&shop_id=xxxx..............
//http://localhost:8088/CategoryController/addCateGory?parent_id=0&category_name=%E6%B5%8B%E8%AF%95&shop_id=1&pic=pic&seq=
   // @RequestMapping(value = "/addCateGory",method = RequestMethod.POST)
    @PostMapping("/addCateGory")
    @ResponseBody
    //当前端用get请求访问此方法，如果携带的请求参数与方法形参javaBean的属性相同时，会自动将参数值赋给javaBean的相应属性
    public JsonResponse addCateGory(CategoryBean categoryBean) {
        System.out.println("接收到前端传递过来的数据为:" + categoryBean);

        JsonResponse jsonResponse = new JsonResponse();
        try{
            int res = categoryService.saveData(categoryBean);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");
        }catch (Exception e){
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("插入数据出现异常:"+e.getMessage());
        }



        return  jsonResponse;
    }



    @RequestMapping("/deleteCategory")
    @ResponseBody
    public  JsonResponse deleteCategory(Integer category_id){
     //   categoryService.
        JsonResponse jsonResponse= new JsonResponse();
      try{
          int res = categoryService.deleteDataById(category_id);
          if (res>0){
              jsonResponse.setMsg("OK");
              jsonResponse.setCode(0);
          }else{
              jsonResponse.setCode(1);
              jsonResponse.setMsg("该数据不存在了");
          }

      }catch (Exception e){
            e.printStackTrace();
          jsonResponse.setCode(1);
          jsonResponse.setMsg(e.getMessage());
      }

      return  jsonResponse;
    }



    @RequestMapping("/updateCateGory")
    @ResponseBody
    public  JsonResponse updateCateGory(CategoryBean categoryBean){
        //   categoryService.
        JsonResponse jsonResponse= new JsonResponse();
        try{
            int res = categoryService.updateData(categoryBean);
            if (res>0){
                jsonResponse.setMsg("OK");
                jsonResponse.setCode(0);
            }else{
                jsonResponse.setCode(1);
                jsonResponse.setMsg("更新失败,该数据不存在");
            }

        }catch (Exception e){
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());
        }

        return  jsonResponse;
    }


}
